package com.tmpt.service.impl;


import java.sql.Timestamp;
import java.text.ParseException;

import javax.annotation.Resource;

import com.tmpt.beans.A1LoginFailure;
import com.tmpt.beans.A2User;
import com.tmpt.dao.intf.IA1LoginFailureDao;
import com.tmpt.dao.intf.IA2UserDao;
import com.tmpt.dto.Result;
import com.tmpt.service.ILoginFailureService;
import com.tmpt.utils.DateTool;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


@Service
@Transactional(propagation=Propagation.REQUIRED)
public class LoginFailureService implements ILoginFailureService {
	
	private Logger log = Logger.getLogger(this.getClass());

    @Resource private IA1LoginFailureDao loginFailureDao;
    @Resource private IA2UserDao userDao;
     

    public Result loginBefore(Object entity,  int hourInt, int limitFailureTimes){
    	Result result = new Result();
    	
    	A1LoginFailure loginFailure = null;
    	if(entity instanceof A2User){
    		A2User emp = ((A2User)entity);
	    	loginFailure = emp.a1LoginFailure;
    	}else{
    		return result.fault("登录次数限制:传入参数无效");
    	}
    	//
    	if(loginFailure==null){
    		return result.success();
    	}
    	if(loginFailure.failureTimes==0){
    		return result.success();
    	}
    	
    	//判断时间间隔，如果可以重新登录的话，将相关数据清空(times, lasttime)
    	Timestamp failureTime = loginFailure.lastFailureTime;
    	String failureTimeStr = DateTool.date2String(failureTime, "yyyy-MM-dd HH:mm:ss");
    	try {
    		//可以解除限制了，时间过了
			if(DateTool.compare2CurDate(failureTimeStr, hourInt)==-1){
				this.loginRelease(entity);
		    	return result.success();
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}

    	if(loginFailure.failureTimes>=limitFailureTimes){
    		try {
				return result.fault("您的帐号已被锁定，请在下列时间之后登录："+DateTool.dateAddHour(DateTool.date2String(loginFailure.lastFailureTime,"yyyy-MM-dd HH:mm:ss"), hourInt));
			} catch (ParseException e) {
				e.printStackTrace();
				return result.fault("您的帐号已被锁定");
			} 
    	}
    	
    	return result.success();
    }
    
    public Result loginFailure(Object entity, int hourInt, int limitFailureTimes){
    	Result result = new Result();
    	StringBuilder sb = new StringBuilder();
    	A1LoginFailure loginFailure = null;
    	
    	if(entity instanceof A2User){
    		A2User emp = ((A2User)entity);
	    	loginFailure = emp.a1LoginFailure;
	    	if(loginFailure==null){
	    		loginFailure = new A1LoginFailure();
	    		loginFailure.loginName = emp.getLoginName();
	    		loginFailure.failureTimes = 1;
	    		loginFailure.lastFailureTime = DateTool.getCurTimestamp();
	    		loginFailure.a2User = emp;
	    		loginFailureDao.create(loginFailure);
	        	return result.success();
	    	}
    	}else{
    		return result.fault("登录次数限制:传入参数无效");
    	}
    	
    	loginFailure.failureTimes = loginFailure.failureTimes+1;
    	loginFailure.lastFailureTime = DateTool.getCurTimestamp();
    	loginFailureDao.update(loginFailure);
    	
    	if(loginFailure.failureTimes<3){
    		return result.success(); 
    	}

    	if(loginFailure.failureTimes>=3){
    		int count = limitFailureTimes - loginFailure.failureTimes;
    		if(count==0){
        		sb.setLength(0);
        		try {
					sb.append("您的帐号已被锁定，请在下列时间之后登录："+DateTool.dateAddHour(DateTool.date2String(loginFailure.lastFailureTime,"yyyy-MM-dd HH:mm:ss"), hourInt));
				} catch (ParseException e) {
					sb.append("您的帐号已被锁定");
				}
    		}else{
        		sb.setLength(0);
        		sb.append("您还有").append(count).append("次登录机会,");
        		sb.append("共").append(limitFailureTimes).append("次登录密码错误后，您的帐号将被锁定");
        		sb.append(hourInt).append("小时");
    		}
    		return result.success(sb.toString()); 
    	}
    	
    	
    	return result.success();
    }
    
    public Result loginRelease(Object entity){
    	Result result = new Result();
    	
    	A1LoginFailure loginFailure = null;
    	if(entity instanceof A2User){
    		A2User emp = ((A2User)entity);
	    	loginFailure = emp.a1LoginFailure;
    	}else{
    		return result.fault("登录次数限制:传入参数无效");
    	}
    	//never failed , no data in LoginFailure
    	if(loginFailure==null){
    		return result.success();
    	}
    	
		loginFailure.failureTimes = 0;
		loginFailure.lastFailureTime = null;
		loginFailureDao.update(loginFailure);

		return result.success();
    }


    @Override
    public Result loginBefore(long customerId, long empId, long merchantInfoId, int hourInt,
    		int limitFailureTimes) {
    	Object entity = null;
    	if(empId!=0){
    		entity = userDao.fetch(empId);
    	}
    	return this.loginBefore(entity, hourInt, limitFailureTimes);
    }

    @Override
    public Result loginFailure(long customerId, long empId, long merchantInfoId, int hourInt,
    		int limitFailureTimes) {
    	Object entity = null;
    	if(empId!=0){
    		entity = userDao.fetch(empId);
    	}
    	
    	return this.loginFailure(entity, hourInt, limitFailureTimes);
    }
    
    @Override
    public Result loginRelease(long customerId, long empId, long merchantInfoId) {
    	Object entity = null;
    	if(empId!=0){
    		entity = userDao.fetch(empId);
    	}
    	return this.loginRelease(entity);
    }
    

    public Result loginBefore(Object customer,A2User emp, int hourInt, int limitFailureTimes){
    	Result result = new Result();
    	
    	A1LoginFailure loginFailure = null;
    	if(emp!=null){
    		loginFailure = emp.a1LoginFailure;
    	}else{
    		return result.fault("登录次数限制:传入参数无效");
    	}
    	//
    	if(loginFailure==null){
    		return result.success();
    	}
    	if(loginFailure.failureTimes==0){
    		return result.success();
    	}
    	
    	//判断时间间隔，如果可以重新登录的话，将相关数据清空(times, lasttime)
    	Timestamp failureTime = loginFailure.lastFailureTime;
    	String failureTimeStr = DateTool.date2String(failureTime,"yyyy-MM-dd HH:mm:ss");
    	try {
    		//可以解除限制了，时间过了
			if(DateTool.compare2CurDate(failureTimeStr, hourInt)==-1){
				this.loginRelease(customer, emp);
		    	return result.success();
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}

    	if(loginFailure.failureTimes>=limitFailureTimes){
    		try {
				return result.fault("您的帐号已被锁定，请在下列时间之后登录："+DateTool.dateAddHour(DateTool.date2String(loginFailure.lastFailureTime,"yyyy-MM-dd HH:mm:ss"), hourInt));
			} catch (ParseException e) {
				e.printStackTrace();
				return result.fault("您的帐号已被锁定");
			} 
    	}
    	
    	return result.success();
    }
    
    public Result loginFailure(Object customer,A2User emp, int hourInt, int limitFailureTimes){
    	Result result = new Result();
    	StringBuilder sb = new StringBuilder();
    	A1LoginFailure loginFailure = null;
    	
    	if(emp!=null){
    		loginFailure = emp.a1LoginFailure;
	    	if(loginFailure==null){
	    		loginFailure = new A1LoginFailure();
	    		loginFailure.loginName = emp.getLoginName();
	    		loginFailure.failureTimes = 1;
	    		loginFailure.lastFailureTime = DateTool.getCurTimestamp();
	    		loginFailure.a2User = emp;
	    		loginFailureDao.create(loginFailure);
	        	return result.success();
	    	}
    	}else{
    		return result.fault("登录次数限制:传入参数无效");
    	}


    	loginFailure.failureTimes = loginFailure.failureTimes+1;
    	loginFailure.lastFailureTime = DateTool.getCurTimestamp();
    	loginFailureDao.update(loginFailure);
    	
    	if(loginFailure.failureTimes<3){
    		return result.success(); 
    	}

    	if(loginFailure.failureTimes>=3){
    		int count = limitFailureTimes - loginFailure.failureTimes;
    		if(count==0){
        		sb.setLength(0);
        		try {
					sb.append("您的帐号已被锁定，请在下列时间之后登录："+DateTool.dateAddHour(DateTool.date2String(loginFailure.lastFailureTime,"yyyy-MM-dd HH:mm:ss"), hourInt));
				} catch (ParseException e) {
					sb.append("您的帐号已被锁定");
				}
    		}else{
        		sb.setLength(0);
        		sb.append("您还有").append(count).append("次登录机会,");
        		sb.append("共").append(limitFailureTimes).append("次登录密码错误后，您的帐号将被锁定");
        		sb.append(hourInt).append("小时");
    		}
    		return result.success(sb.toString()); 
    	}
    	
    	
    	return result.success();
    }
    
    public Result loginRelease(Object customer, A2User emp){
    	Result result = new Result();
    	
    	A1LoginFailure loginFailure = null;
    	if(emp!=null){
    		loginFailure = emp.a1LoginFailure;
    	}else{
    		return result.fault("登录次数限制:传入参数无效");
    	}
    	//never failed , no data in LoginFailure
    	if(loginFailure==null){
    		return result.success();
    	}
    	
		loginFailure.failureTimes = 0;
		loginFailure.lastFailureTime = null;
		loginFailureDao.update(loginFailure);

		return result.success();
    }


}
